import IORedis from 'ioredis'
import { logger } from './logger'

// Redis配置
const redisConfig = {
  host: process.env.REDIS_HOST || 'localhost',
  port: parseInt(process.env.REDIS_PORT || '6379'),
  password: process.env.REDIS_PASSWORD,
  db: parseInt(process.env.REDIS_DB || '0'),
  retryStrategy: (times: number) => {
    const delay = Math.min(times * 50, 2000)
    return delay
  },
  reconnectOnError: (err: Error) => {
    const targetError = 'READONLY'
    if (err.message.includes(targetError)) {
      return true
    }
    return false
  },
}

// 创建Redis客户端
const redis: IORedis = new IORedis(redisConfig)

// 监听Redis连接事件
redis.on('connect', () => {
  logger.info('Redis connected successfully')
})

redis.on('error', (error) => {
  logger.error('Redis connection error:', error)
})

// 导出Redis客户端实例
export { redis }

// 扩展Redis命令接口
export interface RedisCommands {
  // 基础操作
  get(key: string): Promise<string | null>
  set(key: string, value: string): Promise<'OK'>
  setex(key: string, seconds: number, value: string): Promise<'OK'>
  del(key: string): Promise<number>
  exists(key: string): Promise<number>
  expire(key: string, seconds: number): Promise<number>
  ttl(key: string): Promise<number>

  // 计数器
  incr(key: string): Promise<number>
  incrby(key: string, increment: number): Promise<number>
  decr(key: string): Promise<number>
  decrby(key: string, decrement: number): Promise<number>

  // 哈希表
  hget(key: string, field: string): Promise<string | null>
  hset(key: string, field: string, value: string): Promise<number>
  hdel(key: string, field: string): Promise<number>
  hgetall(key: string): Promise<{ [key: string]: string }>
  hmset(key: string, hash: { [key: string]: string }): Promise<'OK'>
  hincrby(key: string, field: string, increment: number): Promise<number>

  // 列表操作
  lpush(key: string, ...values: string[]): Promise<number>
  rpush(key: string, ...values: string[]): Promise<number>
  lpop(key: string): Promise<string | null>
  rpop(key: string): Promise<string | null>
  lrange(key: string, start: number, stop: number): Promise<string[]>
  ltrim(key: string, start: number, stop: number): Promise<'OK'>
  llen(key: string): Promise<number>

  // 集合操作
  sadd(key: string, ...members: string[]): Promise<number>
  srem(key: string, ...members: string[]): Promise<number>
  smembers(key: string): Promise<string[]>
  sismember(key: string, member: string): Promise<number>
  scard(key: string): Promise<number>

  // 有序集合
  zadd(key: string, score: number, member: string): Promise<number>
  zrem(key: string, member: string): Promise<number>
  zrange(key: string, start: number, stop: number): Promise<string[]>
  zrevrange(key: string, start: number, stop: number): Promise<string[]>
  zscore(key: string, member: string): Promise<string | null>
  zcard(key: string): Promise<number>
  zincrby(key: string, increment: number, member: string): Promise<string>

  // 事务
  multi(): any
  exec(): Promise<any[]>

  // 发布订阅
  publish(channel: string, message: string): Promise<number>
  subscribe(channel: string): Promise<void>
}
